package com.xiaoxu.flow;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


public class FlowReducer extends Reducer<Text, FlowEntity, NullWritable, FlowEntity> {

    /**
     * @param key     phone
     * @param values  <FlowEntity,FlowEntity，FlowEntity>
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<FlowEntity> values, Context context) throws IOException, InterruptedException {

        long ups = 0;
        long downs = 0;

        for (FlowEntity entity : values) {
            ups += entity.getUp();
            downs += entity.getDown();
        }

        context.write(NullWritable.get(),new FlowEntity(key.toString(),ups,downs));

    }
}
